//
//  ST_GiftHistoryViewController.swift
//  btvideo
//
//  Created by mac on 2020/7/2.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import MJRefresh

class ST_GiftHistoryViewController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.title_nav_btn.setTitleForAllStates("收礼记录")
        
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 44+statusBarHeight, left: 0, bottom: 0, right: 0))
        }
        
        self.request_data()
    }
    fileprivate func request_data(){
        
        self.currentPage += 1
        
        if self.currentPage == 1{
            self.data_array.removeAll()
            self.tableView.reloadData()
        }
        
        JVrequest(PH.receiveGoodsHistoryApi,parameters: ["page":self.currentPage,"size":20], success: { (json) in
            
            let array = json["data"].arrayValue
            
            let list = array.map { (itemJson) -> ST_GiftHistoryCellModel in
                
                return ST_GiftHistoryCellModel.init(fromJson: itemJson)
            }
            if self.currentPage == 1{
                self.data_array = list
            }else{
                self.data_array += list
            }
            
            self.tableView.reloadData()
            
            self.load_more_option_style(tableView: self.tableView, count: list.count, pageSize: 20) {
                
                self.tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {[weak self] in

                    self?.request_data()
                })
            }
            
            
        }) { (code, err) in
            
            self.toast(text: err)
        }
        
    }
    
    fileprivate var currentPage = 0

//    MARK: - Properties
    fileprivate lazy var tableView:UITableView = {
        
        let control = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        
        control.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: BTSafeAreaBottom + 30 , right: 0)
        
        control.delegate = self
        control.dataSource = self
        
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        
        
        control.register(cellWithClass: ST_GiftHistoryCell.self)
        
        
        control.separatorStyle = .none
        
        control.backgroundColor = UIColor.white
        
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        
        
        control.emptyDataSetSource = self
        
        control.emptyDataSetDelegate = self
        
        
        return control
    }()

    fileprivate var first = true
    
    fileprivate lazy var data_array = [ST_GiftHistoryCellModel]()
    
}

extension ST_GiftHistoryViewController:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return data_array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withClass: ST_GiftHistoryCell.self, for: indexPath)
        
        cell.model = data_array[safe:indexPath.row]
        
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return ST_GiftRankCell.height
    }
    
    
    
}





extension ST_GiftHistoryViewController:DZNEmptyDataSetSource,DZNEmptyDataSetDelegate{
    
    func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
        
        if first{
            self.first = false
            return false
        }else{
            return true
        }
    }
    
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        
        let attrString = NSMutableAttributedString(string: "暂时未收到礼物哦")
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1)]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        return attrString
    }
    
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        
        return ST_ImageUtils.imageName("首页_img_gift_empty")
    }
}
